//
//  XPlanListViewController.m
//  iPlan
//
//  Created by 侯伟 on 2021/8/29.
//

#import "XPlanListViewController.h"
#import "XPlanPageViewController.h"
#import "XPlanEditViewController.h"

#import "QXAdvertManager.h"

@interface XPlanListViewController ()<QXAdvertDelegate>{
    NSArray* _pageInfos;
}

@property(nonatomic,strong)UIView* adView;

@property(nonatomic,strong)UIButton* addBtn;

@end

@implementation XPlanListViewController

- (instancetype)init{
    if(self = [super init]){
        
        [self initPageInfo];
        
        self.titleColorSelected = COLOR_THEME_A;
        self.titleColorNormal = COLOR_GREEN_LIGHT;
        self.titleFontSelected = FONTMID(15);
        self.titleFontNormal = FONTMID(15);
        self.menuViewStyle = WMMenuViewStyleSegmented;
        self.menuItemWidth = (SCREEN_WIDTH-142)/_pageInfos.count;
        self.menuViewLayoutMode = WMMenuViewLayoutModeScatter;
        self.preloadPolicy = WMPageControllerPreloadPolicyNever;
        self.progressColor = COLOR_WHITE;
        self.showOnNavigationBar = YES;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.scrollView.showsHorizontalScrollIndicator = NO;
    
    UIView* adView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, PX(64))];
    adView.myHeight = PX(64);
    [self.view addSubview:adView];
    _adView = adView;
    
    UIButton* addBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addBtn setImage:[UIImage imageNamed:@"a_item_add"] forState:UIControlStateNormal];
    [addBtn addTarget:self action:@selector(addList:) forControlEvents:UIControlEventTouchUpInside];
    addBtn.frame = CGRectMake(self.view.rs_width - 88, CGRectGetMaxY(self.view.frame) - 88 -IPHONE_TAB_BAR_HEIGHT, 88, 88);
    [self.view addSubview:addBtn];
    _addBtn = addBtn;
    
    [self reloadData];
    
    [self loadBannerAd];
}

//banner广告
- (void)loadBannerAd{
    //获取banner广告
    [QXAdvertManager manager].delegate = self;
    [[QXAdvertManager manager] loadBannerAdWithRootViewController:self superView:self.adView];
}

- (void)initPageInfo{
    NSMutableArray* pages = [NSMutableArray array];
    [pages addObject:@{@"type":@(ADateType_Day),@"title":@"日"}];
    [pages addObject:@{@"type":@(ADateType_Week),@"title":@"周"}];
    [pages addObject:@{@"type":@(ADateType_Month),@"title":@"月"}];
    [pages addObject:@{@"type":@(ADateType_Year),@"title":@"年"}];
    _pageInfos = pages;
}

#pragma mark  -- add
- (void)addList:(UIButton*)sender{
    
    XPlanPageViewController* subontroller = (XPlanPageViewController*)self.currentViewController;
    
    XPlanEditViewController* editController = [XPlanEditViewController new];
    editController.operation = EditOperation_Add;
    editController.dateType = subontroller.dateType;
    editController.editBlock = ^(id  _Nullable obj, EditOperation operation) {
        [subontroller reloadData];
    };
    [UIViewController showViewController:editController animated:YES];
}


#pragma mark -- <QXadverSDKDelegate>
//获取广告类型
- (void)QXAdTypeDidLoaded:(ADType)adType success:(BOOL)success{
    if(!success && adType == ADType_Banner){
        self.adView.visibility = MyVisibility_Gone;
    }
}

//加载成功
- (void)QXAdDidLoaded:(ADType)adType adChannel:(ADChannel)adChannel ad:(id)ad{
    if(adType == ADType_Banner){
        self.adView.visibility = MyVisibility_Visible;
    }
}

//加载失败
- (void)QXAdLoadFaild:(ADType)adType adChannel:(ADChannel)adChannel error:(NSError *)error{
    if(adType == ADType_Banner){
        self.adView.visibility = MyVisibility_Gone;
    }
}

//展示失败
- (void)QXAdDisplayFaild:(ADType)adType adChannel:(ADChannel)adChannel error:(NSError *)error{
    if(adType == ADType_Banner){
        self.adView.visibility = MyVisibility_Gone;
    }
}

//关闭
- (void)QXAdDidClosed:(ADType)adType adChannel:(ADChannel)adChannel ad:(id)ad{
    if(adType == ADType_Banner){
        self.adView.visibility = MyVisibility_Gone;
    }
}



#pragma mark -- WMPageControllerDataSource

- (NSInteger)numbersOfChildControllersInPageController:(WMPageController *)pageController {
    return _pageInfos.count;
}

- (NSString *)pageController:(WMPageController *)pageController titleAtIndex:(NSInteger)index {
    
    NSDictionary* pageInfo = _pageInfos[index];
    return pageInfo[@"title"];
}

- (UIViewController *)pageController:(WMPageController *)pageController viewControllerAtIndex:(NSInteger)index {
    
    NSDictionary* pageInfo = _pageInfos[index];
    ADateType dateType = [pageInfo[@"type"] integerValue];

    XPlanPageViewController* subontroller = [XPlanPageViewController new];
    subontroller.dateType = dateType;
    return subontroller;
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForMenuView:(WMMenuView *)menuView {
    return CGRectMake(70, 0, self.view.frame.size.width-140, 36);
}

- (CGRect)pageController:(WMPageController *)pageController preferredFrameForContentView:(WMScrollView *)contentView {
    CGFloat originY = (self.adView.superview && self.adView.superview == self.view)?PX(64) : 0;
    return CGRectMake(0, originY, self.view.frame.size.width, SCREEN_HEIGHT - IPHONE_NAVIGATION_BAR_HEIGHT - IPHONE_TAB_BAR_HEIGHT);
}

@end
